{"componentChunkName":"component---src-templates-post-jsx","path":"/[BOJ]3190/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","excerpt":"문제 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임은 NxN 정사각 보드위에서 진행되고, 몇몇 칸에는 사과가 놓여져 있다. 보드의 상하좌우 끝에 벽이 있다. 게임이 시작할때 뱀은 맨위 맨좌측에 위치하고 뱀의 …","html":"<h2>문제</h2>\n<p>'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다.</p>\n<p>게임은 NxN 정사각 보드위에서 진행되고, 몇몇 칸에는 사과가 놓여져 있다. 보드의 상하좌우 끝에 벽이 있다. 게임이 시작할때 뱀은 맨위 맨좌측에 위치하고 뱀의 길이는 1 이다. 뱀은 처음에 오른쪽을 향한다.</p>\n<p>뱀은 매 초마다 이동을 하는데 다음과 같은 규칙을 따른다.</p>\n<ul>\n<li>먼저 뱀은 몸길이를 늘려 머리를 다음칸에 위치시킨다.</li>\n<li>만약 이동한 칸에 사과가 있다면, 그 칸에 있던 사과가 없어지고 꼬리는 움직이지 않는다.</li>\n<li>만약 이동한 칸에 사과가 없다면, 몸길이를 줄여서 꼬리가 위치한 칸을 비워준다. 즉, 몸길이는 변하지 않는다.</li>\n</ul>\n<p>사과의 위치와 뱀의 이동경로가 주어질 때 이 게임이 몇 초에 끝나는지 계산하라.</p>\n<h2>입, 출력</h2>\n<p>입력</p>\n<ul>\n<li>첫째 줄에 보드의 크기 N이 주어진다. (2 ≤ N ≤ 100) 다음 줄에 사과의 개수 K가 주어진다. (0 ≤ K ≤ 100)</li>\n<li>다음 K개의 줄에는 사과의 위치가 주어지는데, 첫 번째 정수는 행, 두 번째 정수는 열 위치를 의미한다. 사과의 위치는 모두 다르며, 맨 위 맨 좌측 (1행 1열) 에는 사과가 없다.</li>\n<li>다음 줄에는 뱀의 방향 변환 횟수 L 이 주어진다. (1 ≤ L ≤ 100)</li>\n<li>다음 L개의 줄에는 뱀의 방향 변환 정보가 주어지는데,  정수 X와 문자 C로 이루어져 있으며. 게임 시작 시간으로부터 X초가 끝난 뒤에 왼쪽(C가 'L') 또는 오른쪽(C가 'D')로 90도 방향을 회전시킨다는 뜻이다. X는 10,000 이하의 양의 정수이며, 방향 전환 정보는 X가 증가하는 순으로 주어진다.</li>\n</ul>\n<p>출력</p>\n<ul>\n<li>첫째 줄에 게임이 몇 초에 끝나는지 출력한다.</li>\n</ul>\n<h3>📍 <strong>Logic</strong></h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Info {\n    int second;\n    char direction;\n\n    public Info (int second, char direction) {\n        this.second = second;\n        this.direction = direction;\n    }\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>뱀의 방향 전환 정보를 저장하는 클래스</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">class Dummy {\n    Info[] infos;\n    Queue&lt;Pair&gt; queue = new LinkedList&lt;&gt;();\n    int[][] map;\n    int[] rangeX = { -1, 0, 1, 0 };\n    int[] rangeY = { 0, 1, 0, -1 };\n    int mapSize;\n    int directionChanges;\n    int count = 0;\n    int infoIdx = 0;\n    int snakeDir;\n    ...\n\n}</code>\n        </deckgo-highlight-code>\n<ul>\n<li>Dummy 게임에 필요한 로직을 구현하는 클래스</li>\n<li><code class=\"language-text\">map</code> 에서 1은 뱀의 위치, 2는 사과의 위치를 나타낸다.</li>\n</ul>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">private void moveSnake(int x, int y) { ... }</code>\n        </deckgo-highlight-code>\n<ul>\n<li><code class=\"language-text\">Queue</code> 를 이용해서 뱀의 꼬리 정보를 저장하고, 필요에 따라 먼저 들어온 좌표를 out한다.</li>\n<li>뱀의 방향 전환을 위해 <code class=\"language-text\">snakeDir</code> 변수로 뱀의 현재 방향을 저장한다. (0, 1, 2, 3 - 북, 동, 남, 서)</li>\n<li>시간에 따라 <code class=\"language-text\">infos</code> 에서 방향 전환 정보를 확인한다.</li>\n</ul>\n<h3>📄 <strong>CODE</strong></h3>\n<details>\n  <summary>코드 보기/접기💫</summary>\n    <div markdown=\"1\">\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.util.LinkedList;\nimport java.util.Queue;\n\nclass Pair {\n    int x;\n    int y;\n\n    public Pair(int x, int y) {\n        this.x = x;\n        this.y = y;\n    }\n}\n\nclass Info {\n    int second;\n    char direction;\n\n    public Info (int second, char direction) {\n        this.second = second;\n        this.direction = direction;\n    }\n}\n\nclass Dummy {\n    Info[] infos;\n    Queue&lt;Pair&gt; queue = new LinkedList&lt;&gt;();\n    int[][] map;\n    int[] rangeX = { -1, 0, 1, 0 };\n    int[] rangeY = { 0, 1, 0, -1 };\n    int mapSize;\n    int directionChanges;\n    int count = 0;\n    int infoIdx = 0;\n    int snakeDir;\n\n    public Dummy (int N) {\n        mapSize = N;\n        map = new int[N][N];\n    }\n\n    public int game(int directionChanges) {\n        snakeDir = 1;\n        this.directionChanges = directionChanges;\n        moveSnake(0, 0);\n        return count;\n    }\n\n    private void moveSnake(int x, int y) {\n        queue.add(new Pair(x, y));\n        map[x][y] = 1;\n\n        if (infoIdx &lt; directionChanges) accessInfo();\n\n        x += rangeX[snakeDir];\n        y += rangeY[snakeDir];\n        count++;\n\n        if (checkBoundary(x, y)) return;\n        if (map[x][y] == 1) return;\n        if (map[x][y] != 2) cutTail();\n\n        moveSnake(x, y);\n    }\n\n    private void accessInfo() {\n        if (infos[infoIdx].second == count) {\n            updateDir();\n            infoIdx++;\n        }\n    }\n\n    private void updateDir() {\n        if (infos[infoIdx].direction == &#39;L&#39;) {\n            snakeDir = (snakeDir + 3) % 4;\n        } else {\n            snakeDir = (snakeDir + 1) % 4;\n        }\n    }\n\n    private void cutTail() {\n        if (!queue.isEmpty()) {\n            Pair out = queue.poll();\n            map[out.x][out.y] = 0;\n        }\n    }\n\n    private boolean checkBoundary(int x, int y) {\n        return (x &lt; 0 || x &gt;= mapSize || y &lt; 0 || y &gt;= mapSize);\n    }\n}\n\npublic class Main {\n    public static void main(String[] args) throws IOException {\n        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n        int N = Integer.parseInt(br.readLine());\n\n        Dummy dummy = new Dummy(N);\n\n        int appleCount = Integer.parseInt(br.readLine());\n\n        String[] line;\n        for (int i = 0; i &lt; appleCount; i++) {\n            line = br.readLine().split(&quot; &quot;);\n            dummy.map[Integer.parseInt(line[0])-1][Integer.parseInt(line[1])-1] = 2;\n        }\n\n        int directionChanges = Integer.parseInt(br.readLine());\n        dummy.infos = new Info[directionChanges];\n        for (int i = 0; i &lt; directionChanges; i++) {\n            line = br.readLine().split(&quot; &quot;);\n            dummy.infos[i] = new Info(Integer.parseInt(line[0]), line[1].charAt(0));\n        }\n        br.close();\n\n        System.out.println(dummy.game(directionChanges));\n    }\n}</code>\n        </deckgo-highlight-code>\n  \t</div>\n</details>\n<h3>✏️ <strong>Review</strong></h3>\n<ul>\n<li>웹 백엔드 기능을 구현하는 것처럼, 요구사항 하나하나에 해당하는 기능 덩어리를 만들어보자는 생각으로 접근했다. 시뮬레이션은 주어진 로직 그대로 구현만 하면 되는 것 같다.</li>\n</ul>\n<h3>📕 출처</h3>\n<p>Baekjoon : <a href=\"https://www.acmicpc.net/problem/3190\">https://www.acmicpc.net/problem/3190</a></p>","frontmatter":{"title":"👩‍💻 3190. 뱀","date":"March 18, 2022","update":"March 18, 2022","tags":["구현","시뮬레이션","자료구조","덱","큐"],"series":"👩‍💻 BOJ"},"fields":{"slug":"/[BOJ]3190/","readingTime":{"minutes":6.195}}},"seriesList":{"edges":[{"node":{"id":"27733333-3aad-5771-a1a1-27b4de4b4a01","fields":{"slug":"/[BOJ]2225/"},"frontmatter":{"title":"👩‍💻 2225. 합분해"}}},{"node":{"id":"548ba188-2afe-5575-b79f-107c6c65b07d","fields":{"slug":"/[BOJ]1012/"},"frontmatter":{"title":"👩‍💻 1012. 유기농 배추"}}},{"node":{"id":"0c712680-130f-5fa5-8c3b-3632dd5d1af0","fields":{"slug":"/[BOJ]1260/"},"frontmatter":{"title":"👩‍💻 1260. DFS와 BFS"}}},{"node":{"id":"0e07a592-06ce-5e01-b5e1-95398330372e","fields":{"slug":"/[BOJ]1707/"},"frontmatter":{"title":"👩‍💻 1707. 이분그래프"}}},{"node":{"id":"df12bfec-ed5c-5fc4-8442-0a4b3f9759a3","fields":{"slug":"/[BOJ]2668/"},"frontmatter":{"title":"👩‍💻 2668. 숫자 고르기"}}},{"node":{"id":"01bbb6dc-812c-5223-87c5-53bf04c0d0a6","fields":{"slug":"/[BOJ]9466/"},"frontmatter":{"title":"👩‍💻 9466. 텀 프로젝트"}}},{"node":{"id":"abc306b3-c3a6-552a-8979-e82d506b3921","fields":{"slug":"/[BOJ]2146/"},"frontmatter":{"title":"👩‍💻 2146. 다리 만들기"}}},{"node":{"id":"b3394e53-5e9a-595e-8da4-bb3debd220c0","fields":{"slug":"/[BOJ]5014/"},"frontmatter":{"title":"👩‍💻 5014. 스타트 링크"}}},{"node":{"id":"be297703-e778-5727-8019-6954bc95da3a","fields":{"slug":"/[BOJ]11559/"},"frontmatter":{"title":"👩‍💻 11559. Puyo Puyo"}}},{"node":{"id":"ddf11545-d468-5269-94b1-5daa2720e886","fields":{"slug":"/[BOJ]17471/"},"frontmatter":{"title":"👩‍💻 17471. 게리맨더링"}}},{"node":{"id":"8d77114d-fc6e-5de2-a3ca-c3b250425a56","fields":{"slug":"/[BOJ]2437/"},"frontmatter":{"title":"👩‍💻 2437. 저울"}}},{"node":{"id":"9a8942ce-552f-51ae-b304-0d0fab8f2eba","fields":{"slug":"/[BOJ]14891/"},"frontmatter":{"title":"👩‍💻 14891. 톱니바퀴"}}},{"node":{"id":"f1c04e8a-df0a-5294-949f-5f3a483cbcb0","fields":{"slug":"/[BOJ]14503/"},"frontmatter":{"title":"👩‍💻 14503. 로봇 청소기"}}},{"node":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","fields":{"slug":"/[BOJ]3190/"},"frontmatter":{"title":"👩‍💻 3190. 뱀"}}},{"node":{"id":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a","fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},{"node":{"id":"50eef059-4e55-5f14-a9cf-fb01b6506f02","fields":{"slug":"/[BOJ]17779/"},"frontmatter":{"title":"👩‍💻 17779. 게리맨더링 2"}}},{"node":{"id":"aa67d90b-4801-574c-b225-56c23737a5e2","fields":{"slug":"/[BOJ]2579/"},"frontmatter":{"title":"👩‍💻 2579. 계단 오르기"}}},{"node":{"id":"9db6b43b-963a-5ebd-b72b-69936cf37858","fields":{"slug":"/[BOJ]14499/"},"frontmatter":{"title":"👩‍💻 14499. 주사위 굴리기"}}},{"node":{"id":"6a79cddd-dc1b-5e34-8a84-af313c37de0e","fields":{"slug":"/[BOJ]15685/"},"frontmatter":{"title":"👩‍💻 15685. 드래곤 커브"}}},{"node":{"id":"5e001d34-d985-545b-83ec-7f1c7caea286","fields":{"slug":"/[BOJ]17144/"},"frontmatter":{"title":"👩‍💻 17144. 미세먼지 안녕!"}}},{"node":{"id":"1d132410-9377-5e55-85b9-4664ff64de73","fields":{"slug":"/[BOJ]9935/"},"frontmatter":{"title":"👩‍💻 9935. 문자열 폭발"}}},{"node":{"id":"e13ff8d6-9b02-56c0-83e2-1d3144f70e1d","fields":{"slug":"/[BOJ]16472/"},"frontmatter":{"title":"👩‍💻 16472. 고냥이"}}},{"node":{"id":"0f782160-aaa9-5c54-80c0-72a15172dd16","fields":{"slug":"/[BOJ]1083/"},"frontmatter":{"title":"👩‍💻 1083. 소트"}}},{"node":{"id":"fef58615-9e18-5f50-8d09-3d1e2839342f","fields":{"slug":"/[BOJ]20922/"},"frontmatter":{"title":"👩‍💻 20922. 겹치는 건 싫어"}}},{"node":{"id":"7ca89240-c65f-5cb4-aeef-c5f1605ebd00","fields":{"slug":"/[BOJ]16172/"},"frontmatter":{"title":"👩‍💻 16172. 나는 친구가 적다(Large)"}}}]},"previous":{"fields":{"slug":"/[JPA]7/"},"frontmatter":{"title":"📚 7. 고급 매핑"}},"next":{"fields":{"slug":"/[BOJ]14502/"},"frontmatter":{"title":"👩‍💻 14502. 연구소"}}},"pageContext":{"id":"883b6ecf-7528-5c16-92fa-edf12a64e80e","series":"👩‍💻 BOJ","previousPostId":"26e75089-52ac-5b50-98c9-c2806068b034","nextPostId":"099b2576-7d05-5872-a9f0-f1ee6efd3a9a"}},"staticQueryHashes":[]}